package com.qdy.api;

import com.jiai.core.tools.RestResultGenerator;
import com.jiai.exception.ServiceException;

import com.qdy.entity.Follow;
import com.qdy.repository.FollowRepository;
import com.qdy.util.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 关注API接口
 * @version  v1
 * Created by 方礼爱 on 2017/3/26.
 */
@RequestMapping(value = "/api/v1/follow")
@RestController
public class FollowAPIController {

    private static final Logger log= LoggerFactory.getLogger(UserAPIController.class);
    @Autowired
    private FollowRepository followRepository;

    /**
     * 关注
     * @param type 类型
     * @param targetId
     * @return
     */
    @RequestMapping(value="/follow/{type}/{targetId}",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public Object follow(@PathVariable("type") Integer type,@PathVariable("targetId") Long targetId)throws  Exception{

        try{
            Follow follow=new Follow();
            follow.setTyep(type);
            follow.setUserId(HttpUtil.getUserId());
            follow.setTarget(targetId);
            followRepository.save(follow);
            return  RestResultGenerator.genSuccessResult("关注成功");
        }catch (Exception e){
            throw new ServiceException("服务器异常!",e.getMessage());
        }
     }



}
